package com.taotao.search.service.impl;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.jdbc.SQL;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.common.SolrInputDocument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.object.SqlQuery;
import org.springframework.stereotype.Service;

import com.taotao.common.pojo.SearchItem;
import com.taotao.common.pojo.SearchResult;
import com.taotao.common.pojo.TaotaoResult;
import com.taotao.search.dao.SearchDao;
import com.taotao.search.mapper.SearchItemMapper;
import com.taotao.search.service.SearchService;
@Service
public class SearchServiceImpl implements SearchService{

	@Autowired
	public SearchItemMapper mapper;
	
	@Autowired
	private SolrServer solrServer;
	
	@Autowired
	private SearchDao searchDao;
	
	@Override
	public TaotaoResult importAllSearchItems() throws Exception {
		// 1.注入mapper
		//2.调用mapper的方法，查询所有商品的数据
		List<SearchItem> lists = mapper.getSearchItemList();
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
		
		//3.通过solrj将数据写入索引中
			//3.1创建httpsolrserver
			//3.2创建solrinputdocument将数据一个个插入到索引库中
		System.out.println(df.format(new Date()));// new Date()为获取当前系统时间
		for (SearchItem searchItem : lists) {
			SolrInputDocument solrInputDocument = new SolrInputDocument();
			solrInputDocument.addField("id", searchItem.getId().toString());
			solrInputDocument.addField("item_title", searchItem.getTitle());
			solrInputDocument.addField("item_sell_point", searchItem.getSell_point());
			solrInputDocument.addField("item_price", searchItem.getPrice());
			solrInputDocument.addField("item_image", searchItem.getImage());
			solrInputDocument.addField("item_category_name", searchItem.getCategory_name());
			solrInputDocument.addField("item_desc", searchItem.getItem_desc());
			solrServer.add(solrInputDocument);
		}
		System.out.println(df.format(new Date()));// new Date()为获取当前系统时间
		solrServer.commit();
		System.out.println(df.format(new Date()));// new Date()为获取当前系统时间
		return TaotaoResult.ok();
	}
	

	@Override
	public SearchResult search(String queryString, Integer page, Integer rows) throws Exception {
		// 创建solrQuery对象
		SolrQuery aQuery = new SolrQuery();
		//	设置主查询条件
		if(StringUtils.isNoneBlank(queryString)) {
			aQuery.setQuery(queryString);
		}else {
			aQuery.setQuery("*:*");
		}
		//设置过滤条件
		if(page==null) page =1;
		if(rows==null) rows=60;
		aQuery.setStart((page-1)*rows);
		aQuery.setRows(rows);
		
		//设置默认的商品搜索域
		aQuery.set("df","item_keywords");
		//设置高亮
		aQuery.setHighlight(true);
		aQuery.setHighlightSimplePre("<em style=\"color:red\" >");
		aQuery.setHighlightSimplePost("</em>");
		aQuery.addHighlightField("item_title");//设置高亮显示的域
		
		//	调用dao的方法，返回的是SearchResult，只包含了中记录数和list
		SearchResult sResult = searchDao.search(aQuery);
		//	设置SearchResult的总页数
		long pageCount = 0;
		pageCount = sResult.getRecordCount()/rows;
		if(sResult.getRecordCount()%rows>0) {
			pageCount ++;
		}
		sResult.setPageCount(pageCount);
		//	返回
		
		return sResult;
	}


	@Override
	public TaotaoResult updateSearchItemById(Long itemId) throws Exception {
		// TODO Auto-generated method stub
		return searchDao.updateSearchItemById(itemId);
	}

}
